package cn.lx.es.query.processor;

import cn.lx.es.query.annotation.FuzzySearch;
import cn.lx.es.query.annotation.SearchProcessor;
import org.elasticsearch.index.query.BoolQueryBuilder;
import org.elasticsearch.index.query.QueryBuilders;

import java.lang.reflect.Field;

/**
 * 内置处理器 - 模糊搜索处理器
 */
@SearchProcessor
public class SearchFuzzyProcessor implements SearchAnnotationProcessor<FuzzySearch> {
    @Override
    public void process(FuzzySearch annotation, Field field, Object value, BoolQueryBuilder boolQueryBuilder) {
        if (value != null) {
            String fieldName = annotation.value().isEmpty() ? field.getName() : annotation.value();
            boolQueryBuilder.must(QueryBuilders.fuzzyQuery(fieldName, value));
        }
    }
    @Override
    public Class<FuzzySearch> getAnnotationType() {
        return FuzzySearch.class;
    }
}
